package com.ihd.ihardware.view.scooter.view;

import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.databinding.ActivityScPswBinding;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.view.scooter.viewModel.SCViewModel;
import com.jeremyliao.liveeventbus.LiveEventBus;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.utils.BLEUtils;
import cn.wowjoy.commonlibrary.utils.InputTools;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

import static com.ihd.ihardware.common.AppConstans.Write_Character_UUID;
import static com.ihd.ihardware.common.AppConstans.Write_Service_UUID;

public class SCPswActivity extends BaseActivity<ActivityScPswBinding, SCViewModel> {
    private DeviceListRes.DataBean.ListBean mBD;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setLeftImageResource(R.drawable.bike_nb_back);
        binding.mtitlebar.setTitle("修改密码");
        binding.mtitlebar.setTitleColor(getResources().getColor(R.color.C_FFFFFF));

        mBD = (DeviceListRes.DataBean.ListBean) getIntent().getSerializableExtra("sc");
        LiveEventBus.get()
                .with("onNotify", String.class)
                .observe(this, new Observer<String>() {
                    @Override
                    public void onChanged(@Nullable String s) {
                        Log.e("onNotify setting", s);
                        if (s.startsWith("5A0506")) {
                            if (s.startsWith("5A050600")) {
                                ToastUtils.showShort(SCPswActivity.this, "修改密码失败");
                            } else {
                                ToastUtils.showShort(SCPswActivity.this, "修改密码成功");
                                InputTools.HideKeyboard(binding.confirmpsw);
                                finish();
                            }
                        }
                    }
                });

        binding.modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.oldpsw.length() != 4 || binding.newpsw.length() != 4 || binding.confirmpsw.length() != 4)
                    ToastUtils.showShort(v.getContext(), "请输入4位密码");
                else {
                    if (!binding.newpsw.getText().toString().equals(binding.confirmpsw.getText().toString()))
                        ToastUtils.showShort(v.getContext(), "密码确认输入不一致");
                    else
                        BLEUtils.writePsw(mBD.getMac(), "A510060" + binding.oldpsw.getText().toString().substring(0, 1) + "0" + binding.oldpsw.getText().toString().substring(1, 2) + "0" + binding.oldpsw.getText().toString().substring(2, 3) + "0" + binding.oldpsw.getText().toString().substring(3, 4) + "00000" + binding.newpsw.getText().toString().substring(0, 1) + "0" + binding.newpsw.getText().toString().substring(1, 2) + "0" + binding.newpsw.getText().toString().substring(2, 3) + "0" + binding.newpsw.getText().toString().substring(3, 4) + "0000", "", Write_Service_UUID, Write_Character_UUID);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sc_psw;
    }

    @Override
    protected Class getViewModelClass() {
        return SCViewModel.class;
    }
}
